<?php

namespace common\components\system;

use Yii;
use common\models\mongo\AccessLog;

/**
 * Description of AdminLog
 *
 * @author emhome
 */
class AccessRecord extends BaseLogRecord {

    public $modelClass = AccessLog::class;
    public static $ignoreFileTypes = [
        'gif', 'png', 'jpg', 'jpeg', 'css', 'js', 'svg', 'bmp', 'svg'
    ];

    /**
     * 访问日志
     * @param obj $event
     * @return mixed
     */
    public static function afterAction($event) {
        /* @var $sender \yii\web\Application */
        $sender = $event->sender;
        $request = $sender->request;
        $ext = pathinfo($request->url, PATHINFO_EXTENSION);
        if (!Yii::$app->user->isGuest && in_array($ext, static::$ignoreFileTypes)) {
            return;
        }
        $controller = $sender->controller;
        if ($controller instanceof \backend\components\Controller && $controller->recordHistory === false) {
            return;
        }
        $data = [
            'title' => $sender->view->title,
            'controller' => $controller::className(),
            'action' => $sender->requestedAction->id,
            'params' => $sender->requestedParams,
        ];
        static::log($data, $request);
    }

}
